﻿//"The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/

//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.

//The Original Code is TSharp.

//The Initial Developer of the Original Code is Mar3ek (http://mar3ek.wordpress.com).
//Portions created by Mar3ek are Copyright (C) 2010
//Mar3ek (http://mar3ek.wordpress.com). All Rights Reserved.

//Contributor(s): ______________________________________.

//Alternatively, the contents of this file may be used under the terms
//of the _____ license (the  "[___] License"), in which case the
//provisions of [______] License are applicable instead of those
//above.  If you wish to allow use of your version of this file only
//under the terms of the [____] License and not to allow others to use
//your version of this file under the MPL, indicate your decision by
//deleting  the provisions above and replace  them with the notice and
//other provisions required by the [___] License.  If you do not delete
//the provisions above, a recipient may use your version of this file
//under either the MPL or the [___] License."

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace TSharp
{
    public partial class Twitter
    {
        #region Favorites

        #region GetFavorites
        /// <summary>
        /// Returns the 20 most recent favorite statuses for the specified user.
        /// </summary>
        /// <param name="screenName">Screen name of the target user</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns>List of tweets</returns>
        public Collection<Tweet> GetFavorites(string screenName)
        {
            if (string.IsNullOrEmpty(screenName)) throw new ArgumentNullException("screenName");

            var result = SendRequest(RequestMethod.Get,
                                        string.Format("https://api.twitter.com/1/favorites/{0}.xml", screenName), true);

            return ParseTimeline(result);
        }

        /// <summary>
        /// Returns the 20 most recent favorite statuses for the specified user.
        /// </summary>
        /// <param name="userId">User ID of the specified user</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <returns>List of tweets</returns>
        public Collection<Tweet> GetFavorites(ulong userId)
        {
            if (userId < 1) throw new ArgumentOutOfRangeException("userId");

            var result = SendRequest(RequestMethod.Get,
                                        string.Format("https://api.twitter.com/1/favorites/{0}.xml", userId), true);

            return ParseTimeline(result);
        }

        /// <summary>
        /// Returns the 20 most recent favorite statuses for the specified user.
        /// </summary>
        /// <param name="user">The user for whom to retrieve results</param>
        /// <returns>List of tweets</returns>
        /// <exception cref="ArgumentNullException"><paramref name="user" /> is <c>null</c>.</exception>
        public Collection<Tweet> GetFavorites(TwitterUser user)
        {
            if (user == null) throw new ArgumentNullException("user");

            return GetFavorites(user.ScreenName);
        }

        /// <summary>
        /// Returns 20 favorite statuses for the specified user.
        /// </summary>
        /// <param name="screenName">Screen name of the target user</param>
        /// <param name="page">Specifies the page of results to retrieve</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <returns>List of tweets</returns>
        public Collection<Tweet> GetFavorites(string screenName, uint page)
        {
            if (string.IsNullOrEmpty(screenName)) throw new ArgumentNullException("screenName");
            if (page < 1) throw new ArgumentOutOfRangeException("page");

            var result = SendRequest(RequestMethod.Get,
                                     string.Format("https://api.twitter.com/1/favorites/{0}.xml", screenName), true,
                                     new KeyValuePair<string, string>("screen_name", screenName));

            return ParseTimeline(result);
        }

        /// <summary>
        /// Returns 20 favorite statuses for the specified user.
        /// </summary>
        /// <param name="userId">User ID of the specified user</param>
        /// <param name="page">Specifies the page of results to retrieve</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <returns>List of tweets</returns>
        public Collection<Tweet> GetFavorites(uint userId, uint page)
        {
            if (userId < 1) throw new ArgumentOutOfRangeException("userId");
            if (page < 1) throw new ArgumentOutOfRangeException("page");

            var result = SendRequest(RequestMethod.Get,
                                     string.Format("https://api.twitter.com/1/favorites/{0}.xml", userId), true,
                                     new KeyValuePair<string, string>("user_id", userId.ToString()));

            return ParseTimeline(result);
        }

        /// <summary>
        /// Returns the 20 favorite statuses for the specified user.
        /// </summary>
        /// <param name="user">The user for whom to retrieve results</param>
        /// <param name="page">Specifies the page of results to retrieve</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <returns>List of tweets</returns>
        /// <exception cref="ArgumentNullException"><paramref name="user" /> is <c>null</c>.</exception>
        public Collection<Tweet> GetFavorites(TwitterUser user, uint page)
        {
            if (user == null) throw new ArgumentNullException("user");

            return GetFavorites(user.ScreenName, page);
        } 
        #endregion

        #region FavoriteStatus
        /// <summary>
        /// Favorites the status specified in the ID parameter as the authenticating user.
        /// </summary>
        /// <param name="statusId">The numerical ID of the desired status</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <returns>Favorited status</returns>
        public Tweet FavoriteStatus(ulong statusId)
        {
            if (statusId < 1) throw new ArgumentOutOfRangeException("statusId");

            var result = SendRequest(RequestMethod.Post,
                                        string.Format("https://api.twitter.com/1/favorites/create/{0}.xml", statusId),
                                        false);
            var doc = XDocument.Parse(result);

            return ParseStatus(doc.Element("status"));
        }

        /// <summary>
        /// Favorites the specified status as the authenticating user.
        /// </summary>
        /// <param name="status">The status to mark as favorite</param>
        /// <returns>Favorited status</returns>
        /// <exception cref="ArgumentNullException"><paramref name="status" /> is <c>null</c>.</exception>
        public Tweet FavoriteStatus(Tweet status)
        {
            if (status == null) throw new ArgumentNullException("status");

            return FavoriteStatus(status.Id);
        } 
        #endregion

        #region UnfavoriteStatus
        /// <summary>
        /// Un-favorites the status specified in the ID parameter as the authenticating user.
        /// </summary>
        /// <param name="statusId">The numerical ID of the desired status</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <returns>Unfavorited status</returns>
        public Tweet UnfavouriteStatus(ulong statusId)
        {
            if (statusId < 1) throw new ArgumentOutOfRangeException("statusId");

            var result = SendRequest(RequestMethod.Delete,
                                       string.Format("https://api.twitter.com/1/favorites/destroy/{0}.xml", statusId),
                                       false);
            var doc = XDocument.Parse(result);

            return ParseStatus(doc.Element("status"));
        }

        /// <summary>
        /// Un-favorites the specified status.
        /// </summary>
        /// <param name="status">The status to matk as not favorite</param>
        /// <returns>Unfavorited status</returns>
        /// <exception cref="ArgumentNullException"><paramref name="status" /> is <c>null</c>.</exception>
        public Tweet UnfavouriteStatus(Tweet status)
        {
            if (status == null) throw new ArgumentNullException("status");

            return UnfavouriteStatus(status.Id);
        } 

        #endregion

        #endregion
    }
}
